﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AllenalaMente.Games.Sword
{
    public enum TargetType
    {
        normal,
        bomb
    }

    public partial class Target : UserControl
    {
        public TargetType type = TargetType.normal;

        public Point position;
        public Point direction;
        public double speed;
        public bool isActive = true;

        private Random rnd = new Random();

        public Target()
        {
            InitializeComponent();

            this.IsHitTestVisible = false;

            direction = new Point(0.7, -1.0);

            SetType(TargetType.normal);
        }
        
        private Color getColor(int c)
        {
            Color color = new Color();

            switch (c)
            {
                case 0: color = new Color() { R = 100, G = 100, B = 100, A = 255 }; break;
                case 1: color = Colors.Green; break;
                case 2: color = Colors.Blue; break;
                case 3: color = Colors.Magenta; break;
                case 4: color = Colors.Orange; break;
                case 5: color = Colors.Purple; break;
                case 6: color = Colors.Cyan; break;
                case 7: color = Colors.Red; break;
                case 8: color = Colors.Yellow; break;
                case 9: color = Colors.Gray; break;
            }

            return color;
        }

        public void SetType(TargetType t)
        {
            type = t;

            Color1.Color = getColor(rnd.Next(10));
            Color2.Color = getColor(rnd.Next(10));

            switch (type)
            {
                case TargetType.normal:
                    canvasBomb.Visibility = Visibility.Collapsed;
                    break;

                case TargetType.bomb:
                    canvasBomb.Visibility = Visibility.Visible;

                    // Carica Immagine
                    Uri uri = new Uri("/AllenalaMente;component/Assets/Images/Sword/Bomb.png", UriKind.RelativeOrAbsolute);
                    ImageSource img = new System.Windows.Media.Imaging.BitmapImage(uri);
                    imageBomb.SetValue(Image.SourceProperty, img);

                    break;
            }
        }

        public void SetAngle(double a)
        {
            if (type == TargetType.bomb && !isActive) return;

            rotateTransform.Angle = (rotateTransform.Angle + a) % 360;
        }

        public void SetPosition(Point p)
        {
            position = p;
        }

        public void SetImage(string ImagePath)
        {
            // Carica Immagine
            Uri uri = new Uri(ImagePath, UriKind.RelativeOrAbsolute);
            ImageSource img = new System.Windows.Media.Imaging.BitmapImage(uri);
            EllipseImg.SetValue(Image.SourceProperty, img);
        }

        public bool Intersect(Line l)
        {
            bool intersect = true;

            Point p1 = position;
            Point p2 = new Point(position.X + ellipseTarget.Width, position.Y + ellipseTarget.Height);

            if (l.X1 > p2.X && l.X2 > p2.X) return false; // intersect = false; // no intersection (line is to right of rectangle).
            if (l.X1 < p1.X && l.X2 < p1.X) return false; // intersect = false; // no intersection (line is to left of rectangle).
            if (l.Y1 > p2.Y && l.Y2 > p2.Y) return false; // intersect = false; // no intersection (line is above rectangle).
            if (l.Y1 < p1.Y && l.Y2 < p1.Y) return false; // intersect = false; // no intersection (line is below rectangle). 

            return intersect;
        }

        // FADE OUT
        public void Hide()
        {
            DoubleAnimation doubleAnimation = new DoubleAnimation();
            doubleAnimation.From = 1;
            doubleAnimation.To = 0;
            doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5));

            Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Opacity"));
            Storyboard.SetTarget(doubleAnimation, CanvasTarget);

            Storyboard sb = new Storyboard();
            sb.Children.Add(doubleAnimation);
            sb.Begin();

            if (type == TargetType.bomb) Explode();
        }

        public void Explode() 
        {
            // Carica Immagine
            Uri uri = new Uri("/AllenalaMente;component/Assets/Images/Sword/Explosion.png", UriKind.RelativeOrAbsolute);
            ImageSource img = new System.Windows.Media.Imaging.BitmapImage(uri);
            imageBomb.SetValue(Image.SourceProperty, img);
        }

    }
}
